﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Mdb2RIA
{
    public class MdbField
    {
        public MdbField(DataRow data)
        {
            Name = data["ColumnName"].ToString();
            FieldType = Type.GetType(data["DataType"].ToString());
            IsKey = Convert.ToBoolean(data["IsKey"].ToString());
            ReadOnly = Convert.ToBoolean(data["IsAutoIncrement"].ToString()) || Convert.ToBoolean(data["IsReadOnly"].ToString());
        }

        private string[] BuildAttributes()
        {
            List<string> attributes = new List<string>();

            if (IsKey)
            {
                attributes.Add("Key()");
            }
            if (ReadOnly)
            {
                attributes.Add("ReadOnly(true)");
            }

            return attributes.ToArray();
        }

        public string GenerateFieldData()
        {
            return PropertyGenerator.GenerateProperty(
                Name,
                FieldType,
                BuildAttributes()
            );
        }

        public string Name { get; private set; }
        public Type FieldType { get; private set; }
        public bool IsKey { get; private set; }
        public bool ReadOnly { get; private set; }
    }
}
